﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace wpf_print.view
{
    internal class TableView
    {
       
        public static DataGrid GetDataTableDom(JArray listData, List<JObject> list)
        {
            var dataGrid = new DataGrid();
            double turnNum = (double)1.8;
            if (listData == null)
            {
                return dataGrid;
            }
            for (int i = 0; i < list.Count; i++)
            {
                DataGridTemplateColumn dataGridTemplateColumn = new();
                dataGridTemplateColumn.Header = list[i]["name"];
                dataGridTemplateColumn.Width = (double)list[i]["cssStyle"]["width"] + turnNum;//定义每一列的宽度
                dataGridTemplateColumn.MinWidth = (double)list[i]["cssStyle"]["width"] + turnNum;
                dataGridTemplateColumn.MaxWidth = (double)list[i]["cssStyle"]["width"] + turnNum;
                string? path = (string)list[i]["key"];
                Binding binding = new(path)
                {
                    Mode = BindingMode.OneWay
                };//设置每一列的binding
                DataTemplate dataTemplate = new();
                var Item = new FrameworkElementFactory(typeof(TextBlock));
                Item.SetBinding(TextBlock.TextProperty, binding); //数据绑定
                Item.SetValue(TextBlock.TextWrappingProperty, TextWrapping.Wrap); //设置映射值
                Item.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center); //设置映射值
                Item.SetValue(TextBlock.FontSizeProperty, (double)list[i]["cssStyle"]["fontSize"]); //设置映射值
                if ((string)list[i]["cssStyle"]["fontWeight"] == "bold")
                {
                    Item.SetValue(TextBlock.FontWeightProperty, FontWeights.Bold); //设置映射值
                }
                dataTemplate.VisualTree = Item;
                dataGridTemplateColumn.CellTemplate = dataTemplate;
                dataGrid.Columns.Add(dataGridTemplateColumn);
            }
            dataGrid.ItemsSource = listData;
            dataGrid.AutoGenerateColumns = false;//禁止自动添加列
            dataGrid.CanUserAddRows = false;//禁止自动添加行
            dataGrid.RowHeaderWidth = 0;//去除第一列多余的空白
            dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; // 取消滚动条
            dataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; // 取消滚动条
            dataGrid.HorizontalAlignment = HorizontalAlignment.Left; // 取消最后一列空白
            dataGrid.Background = new LinearGradientBrush(Colors.White, Colors.White, 90);
            return dataGrid;
        }
    }
}
